//
//  CBaseController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/1.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "CBaseController.h"

#import "LoadingView.h"
@interface CBaseController ()
@property (nonatomic) BOOL isFirst;
@property (nonatomic, strong) NSString *ctitle;
@property (nonatomic) BOOL ispush;
@end

@implementation CBaseController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initData];
    [self initView];
    _ctitle = self.title;
    if (!_hideToolBar) {
        [self setHideToolBar:_hideToolBar];
    }
}

#pragma mark - 初始化数据
- (void)initData{
    _isFirst = true;
    _hideNavBar = NO;
}

#pragma mark 初始化界面
- (void)initView{
    _hideToolBar = YES;
}

#pragma mark 加载数据
- (void)loadData{
    
}

#pragma mark 重新加载数据
- (void)reloadData{
    [self loadData];
}

#pragma mark 重新加载界面
- (void)reloadView{
    
}

#pragma mark 跳转到下一界面
- (void)pushController:(CBaseController *)controller{
    [self pushController:controller Lever:0 Animated:YES];
}

#pragma mark 跳转界面并做等级先后处理
- (void)pushController:(CBaseController *)controller Lever:(NSInteger)lever Animated:(BOOL)animate{
    UINavigationController *navcontroller = self.navigationController;
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
    if (viewControllers.count == 1 && lever > 0) {
        lever = 0;
    }
    if (lever > viewControllers.count) {
        lever = viewControllers.count - 1;
    }
    for (int i = 0; i < lever; i ++) {
        [viewControllers removeLastObject];
    }
    if(controller)[viewControllers addObject:controller];
    [navcontroller setViewControllers:viewControllers animated:animate];
    _ispush = true;
}

#pragma mark 后退到上一界面
- (void)backController{
    [self pushController:nil Lever:1 Animated:YES];
}

#pragma mark CTabBar Delegate
- (void)CTabBar:(CTabBar *)tabbar selectedIndex:(NSInteger)index{
    
}

-(CTabBar *)mainTabBar{
    static CTabBar *tb;
    if (tb == nil) {
        tb = [[CTabBar alloc] initWithFrame:CGRectMake(0, WindowHeight - 48, WindowWidth, 48)];
        
    }
    if (_hideToolBar == false) {
        [self.view addSubview:tb];
        [tb setDelegate:self];
    }
    return tb;
}

- (void)setIsLoading:(BOOL)isLoading{
    _isLoading = isLoading;
    if (isLoading) {
        //        [self.view addSubview:self.loadingView.superview];
        [[LoadingView shareLoading] startAnimating];
        //        [self.view bringSubviewToFront:self.loadingView.superview];
    }else{
        [[LoadingView shareLoading] stopAnimating];
        //        [[self.loadingView superview] removeFromSuperview];
    }
    
}

- (void)setHasData:(BOOL)hasData{
    _hasData = hasData;
}

- (void)setHideNavBar:(BOOL)hideNavBar{
    [self.navigationController setNavigationBarHidden:hideNavBar animated:NO];
}

- (void)setHideToolBar:(BOOL)hideToolBar{
    _hideToolBar = hideToolBar;
    if (!hideToolBar) {
        [self.mainTabBar setHidden:NO];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    if (!_isFirst) {
        [self reloadData];
    }
    _isFirst = false;
}

- (void) viewWillAppear:(BOOL)animated{
    if (_ctitle) {
        self.title=_ctitle;
    }
    _ispush = self.navigationController.viewControllers.count > 1;
}

- (void)viewWillDisappear:(BOOL)animated{
    if (_ispush) {
        self.title = @"";
    }
    [self setIsLoading:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
